package com.lin.controller;

import com.lin.annotation.MyLog;
import com.lin.pojo.Wares;
import com.lin.pojo.WaresOrder;
import com.lin.services.PurchaseReportService;
import com.lin.services.WaresService;
import com.lin.utils.PageUnit;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * ClassName PurchaseReportController
 * Package com.lin.controller
 *
 * @author hellcat
 * Date 2020/3/18
 * Description:进货报表管理
 * 🐾
 */
@Controller
@RequestMapping("purchaseReport")
public class PurchaseReportController {
    @Autowired
    private PurchaseReportService purchaseReportService;
    @Autowired
    private WaresService waresService;

    @MyLog("查看进货报表")
    @RequiresPermissions("report:purchaseReport:list")
    @RequestMapping(value = "/toPurchaseReport", method = RequestMethod.GET)
    public String toPurchaseReport(HttpSession session, HttpServletRequest request) {
        request.setAttribute("ddId", "PurchaseReport");
        return "purchaseReportMain";
    }

    @RequiresPermissions("report:purchaseReport:list")
    @RequestMapping(value = "/toPurchaseReportDetails", method = RequestMethod.POST)
    public String toPurchaseReportDetails(HttpSession session, HttpServletRequest request) {
        request.setAttribute("ddId", "PurchaseReport");
        String id = request.getParameter("waresId");
        String date = request.getParameter("date");
        request.setAttribute("searchDate", date);
        Wares wares = waresService.getCategoryById(id);

        WaresOrder order = purchaseReportService.getWaresById(id, date);
        request.setAttribute("wares", wares);
        request.setAttribute("order", order);
        return "purchaseReportDetails";

    }

    @RequiresPermissions("report:purchaseReport:list")
    @ResponseBody
    @RequestMapping(value = "/getPurchaseReportDetails", method = RequestMethod.POST)
    public Map<String, Object> getPurchaseReportDetails(HttpServletRequest request) {
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        String searchDate = request.getParameter("searchDate");
        String searchWaresId = request.getParameter("searchWaresId");
        List<WaresOrder> list = purchaseReportService.getPurchaseReportDetails(searchDate, searchWaresId);
        return PageUnit.Page(list, page, limit);
    }

    @RequiresPermissions("report:purchaseReport:list")
    @ResponseBody
    @RequestMapping(value = "/getPurchaseReport", method = RequestMethod.POST)
    public Map<String, Object> getPurchaseReport(HttpServletRequest request) {
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        String searchDate = request.getParameter("searchDate");
        String searchWaresId = request.getParameter("searchWaresId");
        List<WaresOrder> list = purchaseReportService.getPurchaseReport(searchDate, searchWaresId);
        return PageUnit.Page(list, page, limit);
    }

    @RequiresPermissions("report:purchaseReport:list")
    @ResponseBody
    @RequestMapping(value = "/getChartsData", method = RequestMethod.POST)
    public Map<String, Object> getChartsData(HttpServletRequest request) {
        String date = request.getParameter("date");
        return purchaseReportService.getChartsData(date);
    }
}
